package com.miaoquan.mqportal.service.impl;

import com.miaoquan.mqmbg.mapper.UmsMemberMapper;
import com.miaoquan.mqmbg.model.UmsMember;
import com.miaoquan.mqmbg.model.UmsMemberExample;
import com.miaoquan.mqportal.domain.MemberDetails;
import com.miaoquan.mqportal.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 会员管理Service实现类
 *
 * @author Fa
 * @date 2020-03-30 22:29
 */
@Service
public class UmsMemberServiceImpl implements UmsMemberService {

    @Autowired
    private UmsMemberMapper umsMemberMapper;

    @Override
    public UmsMember getByUsername(String username) {
        UmsMemberExample example = new UmsMemberExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<UmsMember> memberList = umsMemberMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(memberList)) {
            return memberList.get(0);
        }
        return null;
    }

    @Override
    public UserDetails loadUserByUsername(String username) {
        UmsMember umsMember = this.getByUsername(username);
        if (umsMember != null) {
            return new MemberDetails(umsMember);
        }
        throw new UsernameNotFoundException("用户名或密码错误");
    }

}
